package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CalculactorActivity extends AppCompatActivity implements View.OnClickListener {
    private  String firstNum="";
    private  String secondNum="";
    private  String operator = "";
    private  String result ="";
    private  String showText ="";
   private   TextView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculactor);
         view = findViewById(R.id.tv_result);
        findViewById(R.id.cancel).setOnClickListener(this);
        findViewById(R.id.Divide).setOnClickListener(this);
        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.reduce).setOnClickListener(this);
        findViewById(R.id.multiply).setOnClickListener(this);
        findViewById(R.id.zero).setOnClickListener(this);
        findViewById(R.id.one).setOnClickListener(this);
        findViewById(R.id.two).setOnClickListener(this);
        findViewById(R.id.three).setOnClickListener(this);
        findViewById(R.id.four).setOnClickListener(this);
        findViewById(R.id.five).setOnClickListener(this);
        findViewById(R.id.six).setOnClickListener(this);
        findViewById(R.id.seven).setOnClickListener(this);
        findViewById(R.id.eight).setOnClickListener(this);
        findViewById(R.id.nine).setOnClickListener(this);
        findViewById(R.id.equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String inputText = ((TextView)v).getText().toString();

        switch (v.getId()){

            case R.id.cancel:
                clear();
                break;
            case R.id.add:
            case R.id.reduce:
            case R.id.multiply:
            case R.id.Divide:
               operator = inputText;
               refreshText(showText+operator);
                 break;
            case R.id.equal:
                double calculated = calculateFour();
                refreshOperate(String.valueOf(calculated));
                refreshText(showText+"="+calculated);
                break;
            default:
                if (operator.equals("")){
                    firstNum = firstNum + inputText;
                }else {
                    secondNum = secondNum + inputText;
                }
                refreshText(showText + inputText);
                break;

        }

    }
    private double calculateFour(){
        switch (operator){
            case "+":
                return Double.parseDouble(firstNum)+Double.parseDouble(secondNum);
            case "-":
                return Double.parseDouble(firstNum)-Double.parseDouble(secondNum);
            case "/":
                return Double.parseDouble(firstNum)/Double.parseDouble(secondNum);
            default:
                return Double.parseDouble(firstNum)*Double.parseDouble(secondNum);

        }
    }
    private void clear(){
        refreshOperate("");
        refreshText("");
    }
    public  void  refreshText(String Text){
        showText = Text;
        view.setText(Text);
    }
    private void refreshOperate(String newResult){
        result  = newResult;
        firstNum = result;
        secondNum = "";
        operator = "";
    }
}